/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.intec.matsab.dao;

import edu.intec.matsab.persistence.Carrera;
import edu.intec.matsab.persistence.PersistenceManager;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

/**
 *
 * @author matilde
 */
public class CarreraDAO extends BasicoDAO {

    public static Carrera buscarPoridCarrera(String idCarrera) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager manager = emf.createEntityManager();

        Carrera carrera = new Carrera();

        try {
            Query query = manager.createNamedQuery("Carrera.findByIdCarrera");
            query.setParameter("idcarrera", idCarrera);
            carrera = (Carrera) query.getSingleResult();

        } catch (PersistenceException exception) {
            exception.printStackTrace();
        } finally {
            manager.close();
        }

        return carrera;
    }

    public static List<Carrera> getAllCarreras() {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager manager = emf.createEntityManager();

        List<Carrera> listCarreras = new ArrayList<Carrera>();

        try {
            Query query = manager.createNamedQuery("Carrera.AllCarreras");
            listCarreras = query.getResultList();

        } catch (PersistenceException exception) {
            exception.printStackTrace();
        } finally {
            manager.close();
        }

        return listCarreras;
    }

    public static boolean eliminar(Carrera obj) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager manager = emf.createEntityManager();

        boolean resultado = false;

        try {

            manager.getTransaction().begin();
            Carrera carrera = manager.getReference(Carrera.class, obj.getIdCarrera());

            manager.remove(carrera);
            manager.getTransaction().commit();

            resultado = true;

        } catch (PersistenceException exception) {
            exception.printStackTrace();
        } finally {
            manager.close();
        }

        return resultado;
    }
}
